package com.gentlemancredit.app.component.keyboard;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.gentlemancredit.app.R;


public class ActionSheetDialog extends Dialog {
    public ActionSheetDialog(@NonNull Context context) {
        super(context);
    }

    public ActionSheetDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ActionSheetDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * @param resource
         * @param gravity  Gravity.CENTER   Gravity.BOTTOM  Gravity.TOP
         * @param height   ViewGroup.LayoutParams.WRAP_CONTENT
         * @param width    ViewGroup.LayoutParams.WRAP_CONTENT
         * @return
         */
        public ActionSheetDialog create(int resource, int gravity, int height, int width) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final ActionSheetDialog dialog = new ActionSheetDialog(context, R.style.Actioon_Sheet_Dialog);
            View layout = inflater.inflate(resource, null);

            Window window = dialog.getWindow();
            if (window != null) {
                window.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams attr = window.getAttributes();
                if (attr != null) {
                    attr.height = height;
                    attr.width = width;
                    //设置dialog 在布局中的位置
                    attr.gravity = gravity;
                    window.setAttributes(attr);
                }
            }
            dialog.setContentView(layout);
            return dialog;
        }

        /**
         * @param resource
         * @param gravity  Gravity.CENTER   Gravity.BOTTOM  Gravity.TOP
         * @return
         */
        public ActionSheetDialog create(int resource, int gravity) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final ActionSheetDialog dialog = new ActionSheetDialog(context, R.style.Actioon_Sheet_Dialog);
            View layout = inflater.inflate(resource, null);

            dialog.setContentView(layout);
            return dialog;
        }
    }


}
